package com.yjc.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author shkstart
 * @create 2022-08-13 12:47
 */
@RestController
@Slf4j
public class FlowLimitController {

    private static int i;

    @GetMapping("/testA")
    public String testA() {
//        try {
//            TimeUnit.MILLISECONDS.sleep(4000);
//        } catch (InterruptedException e) {
//        }
        log.info("testA-->次数：{},{}", i++, Thread.currentThread().getName());
        return "...(￣▽￣) ... testA";
    }

    @GetMapping("/testB")
    public String testB() {
        log.info("testB-->次数：{}", i++);

        return "...(￣▽￣) ... testB";
    }

    @GetMapping("/testD")
    @SentinelResource
    public String testD() {
//        //暂停几秒钟线程
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        log.info("testD 测试RT");

        log.info("test 测试RT");
        int age = 10 / 0;
        return "------testD";
    }

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey", blockHandler = "blockHandler")
    public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                             @RequestParam(value = "p2", required = false) String p2) {
        return "...testHotKey";
    }

    public String blockHandler(String p1, String p2, BlockException b1) {
        return "此应用限流中，请稍后再试.../(ㄒoㄒ)/~~";
    }
}
